// pages/cart/index.js
import { getDefAddress, getCarData, resetCarCheck, delCarGoods, carGoodsNumber } from '../../api/index'
import { hidePhone } from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    goodsList: [],//销售商品
    faileList: [],//下架商品
    total: 0,
    totalPrice: 0,
    allcheck: false,
    addHas: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad: function (options) {
  //   this.defAddress()
  // },
  onShow: function () {
    this.defAddress()
    this.carGoodsList()
  },
  // 获取默认收货地址-未明确-获取地址列表第一项
  defAddress() {
    getDefAddress().then(res => {
      const { info = {} } = res.data
      let dataInfo = [info].map(v => {
        return {
          ...v,
          hidMobile: hidePhone(v.mobile)
        }
      })
      if (res.code === 0) {
        this.setData({ info: dataInfo[0], addHas: true })
      }
    })
  },
  // 购物车内商品列表
  carGoodsList() {
    getCarData().then(res => {
      const { number = 0, price = 0, items = [] } = res.data
      const goodsList = items.filter(v => v.status === 0)
      const checkedList = goodsList.filter(v => v.selected === true)
      this.setData({ total: number, totalPrice: price, goodsList: checkedList })
    })
  },
  // 线上支付
  onlinePay() {
    const { addHas } = this.data
    if (!addHas) {
      showToastUtil('请添加收货地址')
    } else {
    }
  }
})